package com.neusoft.elmboot.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import com.neusoft.elmboot.po.Business;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface BusinessMapper {

	@Select("select * from business where orderTypeId=#{orderTypeId} order by businessId")
	public List<Business> listBusinessByOrderTypeId(Integer orderTypeId);
	
	@Select("select * from business where businessId=#{businessId}")
	public Business getBusinessById(String businessId);

	@Update("update business set businessName = #{userName}, businessImg = #{userImg} where businessId = #{userId}")
    void updateBusinessById(String userId, String userName, String userImg);

	@Update("update business set businessName = #{businessName}, businessAddress = #{businessAddress}, " +
			"businessExplain = #{businessExplain}, starPrice = #{starPrice}, deliveryPrice = #{deliveryPrice} " +
			"where businessId = #{businessId}")
	void updateBusinessInfo(Business business);

	// ==================== 管理员功能 ====================

	/**
	 * 获取商家总数
	 */
	@Select("SELECT COUNT(*) FROM business")
	int getBusinessCount();

	/**
	 * 获取活跃商家数
	 */
	@Select("SELECT COUNT(*) FROM business")
	int getActiveBusinessCount();

	/**
	 * 分页获取商家列表
	 */
	@Select("SELECT businessId, businessName, businessAddress, businessExplain, orderTypeId, starPrice, deliveryPrice " +
			"FROM business ORDER BY businessId LIMIT #{size} OFFSET #{offset}")
	java.util.List<Business> getBusinessesByPage(@org.apache.ibatis.annotations.Param("size") Integer size,
	                                            @org.apache.ibatis.annotations.Param("offset") Integer offset);

	/**
	 * 搜索商家
	 */
	@Select("SELECT businessId, businessName, businessAddress, businessExplain, orderTypeId, starPrice, deliveryPrice " +
			"FROM business WHERE businessName LIKE CONCAT('%', #{keyword}, '%') OR businessId LIKE CONCAT('%', #{keyword}, '%') " +
			"ORDER BY businessId LIMIT #{size} OFFSET #{offset}")
	java.util.List<Business> searchBusinesses(@org.apache.ibatis.annotations.Param("keyword") String keyword,
	                                         @org.apache.ibatis.annotations.Param("size") Integer size,
	                                         @org.apache.ibatis.annotations.Param("offset") Integer offset);

	/**
	 * 搜索商家总数
	 */
	@Select("SELECT COUNT(*) FROM business WHERE businessName LIKE CONCAT('%', #{keyword}, '%') OR businessId LIKE CONCAT('%', #{keyword}, '%')")
	int searchBusinessesCount(String keyword);

	/**
	 * 检查商家是否存在
	 */
	@Select("SELECT COUNT(*) FROM business WHERE businessId = #{businessId}")
	int checkBusinessExists(String businessId);

	/**
	 * 添加商家（管理员）
	 */
	@org.apache.ibatis.annotations.Insert("INSERT INTO business (businessId, businessName, businessAddress, businessExplain, businessImg, orderTypeId, starPrice, deliveryPrice, remarks) " +
			"VALUES (#{businessId}, #{businessName}, #{businessAddress}, #{businessExplain}, #{businessImg}, #{orderTypeId}, #{starPrice}, #{deliveryPrice}, #{remarks})")
	int addBusiness(Business business);

	/**
	 * 更新商家信息（管理员）
	 */
	@Update("UPDATE business SET businessName = #{businessName}, businessAddress = #{businessAddress}, " +
			"businessExplain = #{businessExplain}, orderTypeId = #{orderTypeId}, starPrice = #{starPrice}, " +
			"deliveryPrice = #{deliveryPrice}, remarks = #{remarks} WHERE businessId = #{businessId}")
	int updateBusiness(Business business);

	/**
	 * 删除商家
	 */
	@org.apache.ibatis.annotations.Delete("DELETE FROM business WHERE businessId = #{businessId}")
	int deleteBusiness(String businessId);

	/**
	 * 更新商家状态（预留功能）
	 */
	@Update("UPDATE business SET remarks = #{status} WHERE businessId = #{businessId}")
	int updateBusinessStatus(@org.apache.ibatis.annotations.Param("businessId") String businessId,
	                        @org.apache.ibatis.annotations.Param("status") Integer status);

//	@Select("select password from business where businessId = #{businessId}")
//    String getPasswordByBusinessId(Integer businessId);

//	@Update("update business set password = #{newPassword} where businessId = #{businessId}")
//	void updatePwd(Integer businessId, String newPassword);
}
